/**
 *
 */
package com.mallsite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.RoleService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.mallsite.modules.shop.entity.ShopUser;
import com.mallsite.modules.shop.service.ShopUserService;

/**
 * <p>
 * TODO 描述
 * </p>
 *
 * @author ruyang
 * @since 2020年11月30日
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shopUser")
public class ShopUserController extends BaseController {

	@Autowired
	private ShopUserService shopUserService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;

	@ModelAttribute
	public ShopUser get(String userCode, boolean isNewRecord) {
		return shopUserService.get(userCode, isNewRecord);
	}

	@RequiresPermissions("shop:shopUser:view")
	@RequestMapping(value = "list")
	public String list(ShopUser shopUser, Model model) {
		model.addAttribute("shopUser", shopUser);
		return "modules/shop/shopUserList";
	}

	@RequiresPermissions("shop:shopUser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ShopUser> listData(ShopUser shopUser, HttpServletRequest request, HttpServletResponse response) {
		shopUser.setPage(new Page<>(request, response));
		Page<ShopUser> page = shopUserService.findPage(shopUser);
		return page;
	}

	@RequiresPermissions("shop:shopUser:view")
	@RequestMapping(value = "form")
	public String form(ShopUser shopUser, String op, Model model) {
		
		// 获取当前编辑店铺的角色和权限
		if (StringUtils.inString(op, Global.OP_AUTH)) {
			
			// 获取当前店铺所拥有的角色
			Role role = new Role();
			role.setUserCode(shopUser.getUserCode());
			model.addAttribute("roleList", roleService.findListByUserCode(role));

		}
		
		// 操作类型：add: 全部； edit: 编辑； auth: 授权
		model.addAttribute("op", op);
		model.addAttribute("shopUser", shopUser);
		return "modules/shop/shopUserForm";
	}

	@RequiresPermissions(value={"shop:shopUser:edit","shop:shopUser:authRole"}, logical=Logical.OR)
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ShopUser shopUser, String op, HttpServletRequest request) {
		if (User.isSuperAdmin(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (!ShopUser.USER_TYPE_SHOP.equals(shopUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		ShopUser old = super.getWebDataBinderSource(request);
		if (!Global.TRUE.equals(userService.checkLoginCode(old != null ? old.getLoginCode() : "", shopUser.getLoginCode()))) {
			return renderResult(Global.FALSE, text("保存店铺失败，登录账号''{0}''已存在", shopUser.getLoginCode()));
		}
		Subject subject = UserUtils.getSubject();
		if (StringUtils.inString(op, Global.OP_ADD, Global.OP_EDIT) && subject.isPermitted("shop:shopUser:edit")){
			shopUserService.save(shopUser);
		}
		if (StringUtils.inString(op, Global.OP_ADD, Global.OP_AUTH) && subject.isPermitted("shop:shopUser:authRole")){
			userService.saveAuth(shopUser);
		}
		return renderResult(Global.TRUE, text("保存店铺''{0}''成功", shopUser.getUserName()));
	}
	
	/**
	 * 停用店铺
	 * @param shopUser
	 * @return
	 */
	@RequiresPermissions("shop:shopUser:updateStatus")
	@ResponseBody
	@RequestMapping(value = "disable")
	public String disable(ShopUser shopUser) {
		if (User.isSuperAdmin(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (!ShopUser.USER_TYPE_SHOP.equals(shopUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (shopUser.getCurrentUser().getUserCode().equals(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, text("停用店铺失败，不允许停用当前店铺"));
		}
		shopUser.setStatus(User.STATUS_DISABLE);
		shopUserService.updateStatus(shopUser);
		return renderResult(Global.TRUE, text("停用店铺''{0}''成功", shopUser.getUserName()));
	}
	
	/**
	 * 启用店铺
	 * @param shopUser
	 * @return
	 */
	@RequiresPermissions("shop:shopUser:updateStatus")
	@ResponseBody
	@RequestMapping(value = "enable")
	public String enable(ShopUser shopUser) {
		if (User.isSuperAdmin(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (!ShopUser.USER_TYPE_SHOP.equals(shopUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		shopUser.setStatus(User.STATUS_NORMAL);
		shopUserService.updateStatus(shopUser);
//		AuthorizingRealm.isValidCodeLogin(shopUser.getLoginCode(), shopUser.getCorpCode_(), null, "success");
		return renderResult(Global.TRUE, text("启用店铺''{0}''成功", shopUser.getUserName()));
	}
	
	/**
	 * 密码重置
	 * @param shopUser
	 * @return
	 */
	@RequiresPermissions("shop:shopUser:resetpwd")
	@RequestMapping(value = "resetpwd")
	@ResponseBody
	public String resetpwd(ShopUser shopUser) {
		if (User.isSuperAdmin(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (!ShopUser.USER_TYPE_SHOP.equals(shopUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		userService.updatePassword(shopUser.getUserCode(), null);
//		AuthorizingRealm.isValidCodeLogin(shopUser.getLoginCode(), shopUser.getCorpCode_(), null, "success");
		return renderResult(Global.TRUE, text("重置店铺''{0}''密码成功", shopUser.getUserName()));
	}

	/**
	 * 删除店铺
	 * @param shopUser
	 * @return
	 */
	@RequiresPermissions("shop:shopUser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ShopUser shopUser) {
		if (User.isSuperAdmin(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (!ShopUser.USER_TYPE_SHOP.equals(shopUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此店铺！");
		}
		if (shopUser.getCurrentUser().getUserCode().equals(shopUser.getUserCode())) {
			return renderResult(Global.FALSE, text("删除店铺失败，不允许删除当前店铺"));
		}
		shopUserService.delete(shopUser);
		return renderResult(Global.TRUE, text("删除店铺'{0}'成功", shopUser.getUserName()));
	}
}
